home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / asmutil / afloat.zip / FMULT.ASM < prev    next >
Assembly Source File  |  1988-03-15  |  3KB  |  148 lines

  1.         PAGE ,132
  2. ;----------------------------------------------------------
  3. ; FMULT -- version for use with assembly-language programs
  4. ;
  5. ; Copyright Bob Kline 1988
  6. ;
  7. ; Purpose:
  8. ;       Multiply two single-precision floating-point numbers.
  9. ;
  10. ; Input:
  11. ;       DX:AX contain multiplicand in single-precision IEEE
  12. ;       format; CX:BX contain multiplier, same format.
  13. ;
  14. ; Output:
  15. ;       Product (IEEE) single-precision real in DX:AX.
  16. ;
  17. ; Other registers used:
  18. ;    Values changed in BP, SI, DI, BX, CX
  19. ;
  20. ; Procedures called:
  21. ;       None.
  22. ;
  23. ; Comments:
  24. ;       Sets variable _errno to ERANGE if overflow
  25. ;    occurs.  If a calling routine will be testing
  26. ;       _errno, it must first reset the variable to
  27. ;       zero to be sure that an error code is not
  28. ;       left over from some previous call.
  29. ;----------------------------------------------------------
  30.  
  31.     .MODEL    SMALL
  32.  
  33.     PUBLIC    FMULT
  34.         EXTRN   _errno:WORD
  35.  
  36. EXPONENT    EQU    BP
  37. ERANGE        EQU    34
  38.  
  39.     .CODE
  40.  
  41. FMULT    PROC
  42.  
  43. ; get sign of result and save it
  44.     MOV    SI,DX
  45.     XOR    DX,CX
  46.     AND    DX,8000h
  47.     PUSH    DX
  48.     MOV    DX,SI
  49.  
  50. ; add exponents, save result
  51.     MOV    EXPONENT,CX
  52.     MOV    DI,DX
  53.     SHL    DX,1
  54.     SHL    CX,1
  55.     XCHG    DH,DL
  56.     XCHG    CH,CL
  57.     XOR    DH,DH
  58.     XOR    CH,CH
  59.     SUB    CX,127
  60.     SUB    DX,127
  61.     ADD    CX,DX
  62.     XCHG    EXPONENT,CX
  63.     MOV    DX,DI
  64.  
  65. ; unpack mantissas
  66.     AND    DX,7Fh
  67.     AND    CX,7Fh
  68.     OR    DX,80h
  69.     OR    CX,80h
  70.  
  71. ; multiply DX:AX by CX:BX; since only 24 bits each in multiplier
  72. ;   and multiplicand, result will have 47 or 48 bits, so we would
  73. ;   only need 3 registers for the result -- but we actually only
  74. ;   need 2 since final mantissa can only use the most significant
  75. ;   24 bits.  We will look at the 25th bit for rounding decision
  76. ;   before throwing it away
  77.     PUSH    DX
  78.     PUSH    DX
  79.     PUSH    AX
  80.     MUL    BX        ; A x B
  81.     MOV    SI,DX
  82.     POP    AX
  83.     MUL    CX        ; A x C
  84.     ADD    SI,AX
  85.     MOV    DI,DX
  86.     ADC    DI,0
  87.     POP    AX
  88.     MUL    BX        ; D x B
  89.     ADD    SI,AX
  90.     ADC    DI,DX
  91.     POP    AX
  92.     MUL    CX        ; D x C
  93.     ADD    DI,AX
  94.     MOV    AX,SI
  95.     MOV    DX,DI
  96.  
  97. ; if result is 47 bits, shift it up into the 48th bit
  98.     JS    L0
  99.     SHL    AX,1
  100.     RCL    DX,1
  101.     JMP    SHORT L1
  102. L0:    INC    EXPONENT
  103.  
  104. ; quick 8-bit left shift
  105. L1:    XCHG    AH,AL
  106.     XCHG    DL,AH
  107.     XCHG    DH,DL
  108.  
  109. ; round up if necessary
  110.     TEST    DH,80h
  111.     JZ    L2
  112.     AND    DX,0FFh
  113.     INC    AX
  114.     JNC    L2
  115.     INC    DX
  116.  
  117. ; and adjust exponent if we rounded all the way through the top bit
  118.     TEST    DH,1
  119.     JZ    L2
  120.     INC    EXPONENT
  121.  
  122. ; make the top bit invisible
  123. L2:    AND    DX,7Fh
  124.  
  125. ; get the exponent back
  126.     MOV    BX,EXPONENT
  127.     ADD    BX,127
  128.  
  129. ; check to make sure the exponent isn't out of range
  130.     OR    BH,BH
  131.     JZ    L3
  132.     MOV    _errno,ERANGE
  133.     XOR    BH,BH
  134.  
  135. ; get back into position for packing and fold it in
  136. L3:    XCHG    BH,BL
  137.     SHR    BX,1
  138.     OR    DX,BX
  139.  
  140. ; get the sign back and return
  141.     POP    CX
  142.     OR    DX,CX
  143.     RET
  144.  
  145. FMULT    ENDP
  146.  
  147.     END
  148.